/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


//命名规则：下划线  a_b 变量全小写 
//函数没有下划线，使用驼峰命名 setUp

import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;

/**
 *
 * @author cloudnine
 */
public class Maliang_initial_ implements PlugInFilter{

    public static ImagePlus img_plus;
    public static ImageProcessor img_prpcessor;
    public static ImageStack img_stack;
    


    //setting OPTION_ parameter 
    //detector 探测器类型
    public static boolean OPTION_detectorType; // false: EMCCD; true: sCMOS or CCD
    //SYSTEM options 系统参数
    public static double OPTION_PixelSizeRawImage, OPTION_GrayToPhoton, OPTION_QE, OPTION_Offset, OPTION_PSFWinLength;
    // single-molecule detection options 分子检测参数
    public static double OPTION_BgThreshold, OPTION_MaxPhotonThreshold, OPTION_MinPhotonThreshold,OPTION_MaxPrecisionThreshold, OPTION_MinFWHMThreshold, OPTION_MaxFWHMThreshold;
    // display options 
    public static double OPTION_show, OPTION_PixelSizeRenderedImage;
    // GPU options
    public static int OPTION_noFramePerCycle;
     
    //malloc image parameter
    public static int IMAGE_numFrame, IMAGE_imageW, IMAGE_imageH;	
        
    //malloc GPU_ parameter 根据GPU信息设定的参数
    public static double[] GPU_deviceMemorySize = new double[1];
    public static int GPU_imageConvW;
    public static int GPU_imageConvH;
    public static int GPU_maxFramePerCycle;
    
    @Override
    public int setup(String string, ImagePlus implus) {
        this.img_plus = implus;  
        return 0;
    }
    
    @Override
    public void run(ImageProcessor ip) {
        this.img_prpcessor = ip;
        this.img_stack = img_plus.getImageStack();

         //initial OPTION_ parameter  
        OPTION_detectorType = false;
        OPTION_QE = 0.72;
        OPTION_PixelSizeRawImage = 130;
        OPTION_GrayToPhoton = 0.67;
        OPTION_Offset = 100;
        OPTION_PSFWinLength = 3;
        OPTION_BgThreshold = 5; //阈值系数
        OPTION_MinPhotonThreshold = 500;
        OPTION_MaxPhotonThreshold = 20000;
        OPTION_MinFWHMThreshold = 80;
        OPTION_MaxFWHMThreshold = 250;
        OPTION_MaxPrecisionThreshold = 50;
        OPTION_noFramePerCycle = 1000;
        OPTION_PixelSizeRenderedImage = 18.6;   
        OPTION_show = 1;
        
        
         //initial IMAGE_ parameter
        IMAGE_numFrame = img_plus.getStackSize();
        IMAGE_imageW = img_plus.getWidth();
        IMAGE_imageH = img_plus.getHeight();
             
        
        //初始送入GPU处理的参数
        GPU_imageConvW = ((Maliang_initial_.IMAGE_imageW + 63) / 64) * 64;                     //64倍数
        CUDADLL_.DevicePro(GPU_deviceMemorySize);
        GPU_maxFramePerCycle = (int) (((GPU_deviceMemorySize[0] - 100)) * 1024 * 1024 / (128 * GPU_imageConvW * Maliang_initial_.IMAGE_imageH));
        GPU_imageConvH = (Maliang_initial_.IMAGE_imageH * GPU_maxFramePerCycle + 15) / 16 * 16; //16倍数
        
        //statrRun();
        Maliang_runner_.statrRun();
    }    
    
}
